<?php
ini_set('display_errors','Off');
error_reporting(E_ALL^E_NOTICE);
// header("Content-type:text/html;charset=gb2312");

require_once 'DBDriver/M.php';
$ob = M("subject");



// $s = file_get_contents("php://input");
$params = json_decode(file_get_contents('php://input'),true);

$s = $params['key'];
$search = $params['key'];
$search = explode(' ', $s);
foreach($search as $key=>$value){
    if(empty(trim($value))){
        unset($search[$key]);
    }
    $search[$key] = trim($value);
}

$where = "1";
$hlSearch = array();
foreach($search as $key=>$value){
    $where .= " and `data` like '%{$value}%'";
    $hlSearch[] = "~#@#{$value}@#@~";
}

if(empty($search)){
    json_encode(array('records'=>array()));
    exit();
}
// $list = $ob->where(array('data'=>array('like',"%$search%")))->limit(0,30)->select();
$list = $ob->where($where)->limit(0,30)->select();

foreach($list as $key=>$value){
    $tmp = $ob->where(" id > '{$value['id']}'")->order(array('id'=>'asc'))->limit(0,10)->select();
//     $tmp1 = '';
    foreach($tmp as $key1=>$value1){
        if(substr($value1, 0,3)=='###'){
            unset($tmp[$key1]);
        }
    }
    $list[$key]['data'] = str_replace($search, $hlSearch, $value['data']);
    $list[$key]['more'] = $tmp;
}
if(empty($list)){
    $list = array();
}
// var_dump($list);
echo json_encode(array('records'=>$list));



